package com.jk.hly.controller;

import com.jk.hly.bean.HlyUser;
import com.jk.hly.bean.Tree;
import com.jk.hly.service.HlyTreeService;
import com.jk.hly.util.*;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("tree")
public class HlyTreeController {

    @Resource
    private HlyTreeService  treeService;

    @Resource
    RedisTemplate<String, List<Tree>> redisTemplate;


    @ResponseBody
    @RequestMapping("queryTree")
    public List<Tree> queryTree(){
        List<Tree> tree=null;
        if(redisTemplate.hasKey(Constant.bookstrap_tree)){
            tree=redisTemplate.opsForValue().get(Constant.bookstrap_tree);
        }else{
            tree=treeService.queryTree();
            redisTemplate.opsForValue().set(Constant.bookstrap_tree, tree,10, TimeUnit.SECONDS);
        }
        return tree;
    }

    @ResponseBody
    @RequestMapping("queryUserPhone")
    public SendPage queryUserPhone(ReceivePage page){
        return treeService.queryUserPhone(page);
    }

    @ResponseBody
    @RequestMapping("deleteUserPhone")
    public  String  deleteUserPhone(String  ids){
        treeService.deleteUserPhone(ids);
        return "";
    }

    @ResponseBody
    @RequestMapping("queryById")
    public HlyUser queryById(String  id){
        return  treeService.queryById(id);
    }

    @ResponseBody
    @RequestMapping("updateUserInfo")
    public  String updateUserInfo(HlyUser  user){
        treeService.updateUserInfo(user);
        return "";
    }

    @ResponseBody
    @RequestMapping("addUserPhone")
    public String addUserPhone(HlyUser  user){
        treeService.addUserPhone(user);
        return  "";
    }

    @ResponseBody
    @RequestMapping("daoChu")
    public String daoChu(String ids, HttpServletRequest request){
        String sheetName="手机号";
        String titleName="手机号黑名单";
        String[] headers = {"ID","用户名称","用户账号","密码","手机号", "手机号状态"};
        List<HlyUser> dataSet = treeService.queryAllUser(ids);
        FileSystemView fsv = FileSystemView.getFileSystemView();
        File com = fsv.getHomeDirectory();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HHmmss");
        String format = simpleDateFormat.format(new Date());
        String resultUrl=com.getPath()+"/"+format+".xlsx";
        String pattern="yyyy-MM-dd";
        ExportExcel.exportExcel(sheetName, titleName, headers, dataSet, resultUrl, pattern);
        return "success";
    }


    @ResponseBody
    @RequestMapping("daoRu")
    public String daoRu(String originUrl) throws IOException {
        int startRow=2;
        int endRow=0;
        FileSystemView fsv = FileSystemView.getFileSystemView();
        File com = fsv.getHomeDirectory();
        int ul=originUrl.lastIndexOf("\\");
        String  uu=originUrl.substring(ul);
        originUrl = com +"\\"+uu;
        List<HlyUser> bookList = (List<HlyUser>) importExcel.importExcel(originUrl, startRow, endRow, HlyUser.class);
        for (HlyUser u : bookList) {
            treeService.insertPhone(u);
        }
        return "1";
    }
}
